package com.zjdiepu.www.biz.system;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.system.SysAppRoleMenuMapper;
import com.zjdiepu.www.ibiz.system.ISysAppRoleMenuService;
import com.zjdiepu.www.model.system.dto.SysAppRoleMenu;
import com.zjdiepu.www.model.system.ext.SysAppRoleMenuExt;

/**
 * sys_app_角色菜单关联表Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年10月30日 17时22分<br/>
 * @since 1.0<br/>
 */
@Service
public class SysAppRoleMenuServiceImpl extends BaseService implements ISysAppRoleMenuService {
	public static final Logger logger = LoggerFactory.getLogger(SysAppRoleMenuServiceImpl.class);

	@Autowired
	private SysAppRoleMenuMapper sysAppRoleMenuMapper;
	
	@Override
	public void saveUpdateAndDelete(SysAppRoleMenu sysAppRoleMenu, OperateType operType) {
		switch (operType) {
		case add:
			sysAppRoleMenu.initInsertData(sysAppRoleMenu.getModifier(), sysAppRoleMenu.getModifierName());
			sysAppRoleMenuMapper.insert(sysAppRoleMenu);
			break;
		case edit:
			sysAppRoleMenu.initUpdateData(sysAppRoleMenu.getModifier(), sysAppRoleMenu.getModifierName());
			// 影响的记录数
			int updateCount = sysAppRoleMenuMapper.updateByPrimaryKeySelective(sysAppRoleMenu);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			sysAppRoleMenu.initDeleteData(sysAppRoleMenu.getModifier(), sysAppRoleMenu.getModifierName());
			// 影响的记录数
			int deleteCount = sysAppRoleMenuMapper.updateByPrimaryKeySelective(sysAppRoleMenu);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<SysAppRoleMenu> selectByExt(SysAppRoleMenuExt sysAppRoleMenuExt) {
		return sysAppRoleMenuMapper.selectByExt(sysAppRoleMenuExt);
	}
	
	@Override
	public int countByExt(SysAppRoleMenuExt sysAppRoleMenuExt){
		return sysAppRoleMenuMapper.countByExt(sysAppRoleMenuExt);
	}
	
	@Override
	public SysAppRoleMenu selectById(String id) {
		return sysAppRoleMenuMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			sysAppRoleMenuMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(SysAppRoleMenu sysAppRoleMenu) {
		return sysAppRoleMenuMapper.countRefined(sysAppRoleMenu);
	}

	@Override
	public SysAppRoleMenu selectRefined(SysAppRoleMenu sysAppRoleMenu) {
		return sysAppRoleMenuMapper.selectRefined(sysAppRoleMenu);
	}
	
	@Override
	public int deletedRefined(SysAppRoleMenu sysAppRoleMenu) {
		return sysAppRoleMenuMapper.deletedRefined(sysAppRoleMenu);
	}

	@Override
	public boolean repeat(SysAppRoleMenu sysAppRoleMenu) {
		int count = countRefined(sysAppRoleMenu);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(sysAppRoleMenu.getId())) {
			return true;
		}
		SysAppRoleMenu dbItem = selectRefined(sysAppRoleMenu);
		if(null == dbItem) {
			return false;
		}
		return !sysAppRoleMenu.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return sysAppRoleMenuMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<SysAppRoleMenu> sysAppRoleMenus) {
		return sysAppRoleMenuMapper.insertBatch(sysAppRoleMenus);
	}
}
